/**
 * CFieldComparator.java
 *
 */
package org.apache.lucene.search;

import java.io.IOException;

import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.search.FieldCache.Bytes;
import org.apache.lucene.search.FieldCache.Floats;
import org.apache.lucene.search.FieldCache.Ints;
import org.apache.lucene.search.FieldCache.Longs;

/**
 * 用AtomicReader对目标域的数据的读取
 */
public abstract class CFieldComparator {

    public abstract void setNextReader(AtomicReader reader) throws IOException;

    public abstract Comparable value(int slot);

    public static final class LongComparator extends CFieldComparator {
        private Longs currentReaderValues;

        private final String field;

        LongComparator(String field) {
            this.field = field;
        }

        @Override
        public void setNextReader(AtomicReader reader) throws IOException {
            currentReaderValues = FieldCache.DEFAULT.getLongs(reader, field, false);
        }

        @Override
        public Comparable value(int slot) {
            return currentReaderValues.get(slot);
        }
    }
    
    public static final class FloatComparator extends CFieldComparator {
        private Floats currentReaderValues;

        private final String field;

        FloatComparator(String field) {
            this.field = field;
        }

        @Override
        public void setNextReader(AtomicReader reader) throws IOException {
            currentReaderValues = FieldCache.DEFAULT.getFloats(reader, field, false);
        }

        @Override
        public Comparable value(int slot) {
            return currentReaderValues.get(slot);
        }
    }

    public static final class IntComparator extends CFieldComparator {
        private Ints currentReaderValues;

        private final String field;

        IntComparator(String field) {
            this.field = field;
        }

        @Override
        public void setNextReader(AtomicReader reader) throws IOException {
            currentReaderValues = FieldCache.DEFAULT.getInts(reader, field, false);
        }

        @Override
        public Comparable value(int slot) {
            return currentReaderValues.get(slot);
        }
    }
    
     public static final class StringComparator extends CFieldComparator {
     private Bytes currentReaderValues;
    
     private final String field;
    
     StringComparator(String field) {
     this.field = field;
     }
    
     @Override
     public void setNextReader(AtomicReader reader) throws IOException {
     currentReaderValues = FieldCache.DEFAULT.getBytes( reader, field,false);
     }
    
     @Override
     public Comparable value(int slot) {
     return Byte.valueOf(currentReaderValues.get(slot)).toString();
     }
     }

}
